home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Musik / Misc / Amster / Source / channellist.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-01  |  6.9 KB  |  233 lines

  1. /*
  2. ** Amster - Channel List
  3. ** by Jacob Laursen <laursen@myself.com>
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include <proto/dos.h>
  11. #include <proto/socket.h>
  12. #include <proto/utility.h>
  13.  
  14. #include <netdb.h>
  15. #include <sys/time.h>
  16. #include <sys/socket.h>
  17. #include <sys/ioctl.h>
  18. #include <netinet/tcp.h>
  19. #include <bsdsocket/socketbasetags.h>
  20. #include <error.h>
  21. #include <time.h>
  22.  
  23. #include <MUI/NListview_mcc.h>
  24.  
  25. #include "include/config.h"
  26. #include "include/chat.h"
  27. #include "include/channellist.h"
  28. #include "include/gui.h"
  29. #include "include/info.h"
  30. #include "include/mui.h"
  31. #include "include/prefs.h"
  32.  
  33. #include "include/protos.h"
  34. #include "amster_Cat.h"
  35.  
  36. /* Private prototypes */
  37.  
  38. MUIF ChannelListDisplay(REG(a2) char **array, REG(a1) struct ChannelEntry *entry);
  39. MUIF ChannelListCompare(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_CompareMessage *ncm);
  40. MUIF ChannelListDestruct(REG(a2) APTR pool, REG(a1) struct ChannelEntry *entry);
  41.  
  42.  
  43. MUIF ChannelListDispatch(REG(a0) struct IClass *cl, REG(a2) Object *obj, REG(a1) Msg msg)
  44. {
  45.     struct ChannelListData *data;
  46.  
  47.     switch (msg->MethodID) {
  48.         case OM_NEW:
  49.             return(ChannelListNew(cl, obj, (APTR)msg));
  50.         case CHANLIST_UPDATE:
  51.             {
  52.             u_long item;
  53.             data = INST_DATA(cl, obj);
  54.  
  55.             set(data->LV_Channel, MUIA_NList_Quiet, MUIV_NList_Quiet_Visual);
  56.             while (1) {
  57.                 DoMethod(data->LV_Channel, MUIM_NList_GetEntry, 0, &item);
  58.                 if (!item) break;
  59.                 DoMethod(data->LV_Channel, MUIM_NList_Remove, MUIV_NList_Remove_First);
  60.             }
  61.             set(data->LV_Channel, MUIA_NList_Quiet, MUIV_NList_Quiet_None);
  62.             nap_sendbuf(NAPC_LIST_CHANNELS, "");
  63.             break;
  64.             }
  65.         case CHANLIST_JOIN:
  66.             {
  67.             channel c = NULL;
  68.             struct ChannelEntry *entry;
  69.             Object *win;
  70.             data = INST_DATA(cl, obj);
  71.  
  72.             DoMethod(data->LV_Channel, MUIM_NList_GetEntry, MUIV_NList_GetEntry_Active, &entry);
  73.             if (entry) {
  74.                 win = NewObject(gui->chat_mcc->mcc_Class, NULL, TAG_DONE);
  75.                 if (!win) return NULL;
  76.                 DoMethod(gui->app, OM_ADDMEMBER, win);
  77.                 set(win, MUIA_Window_Open, TRUE);
  78.                 DoMethod(win, CHAT_JOINCHANNEL, entry->Name);
  79.             }
  80.             return NULL;
  81.             }
  82.         case CHANLIST_ENTRY:
  83.             {
  84.             struct ChannelEntry *entry;
  85.             data = INST_DATA(cl, obj);
  86.  
  87.             if (entry = malloc(sizeof(struct ChannelEntry))) {
  88.                 entry->Name = strdup((char *)(((muimsg)msg)->arg1));
  89.                 entry->NumUsers = (int)(((muimsg)msg)->arg2);
  90.                 entry->Topic = strdup((char *)(((muimsg)msg)->arg3));
  91.                 DoMethod(data->LV_Channel, MUIM_NList_InsertSingle, entry, MUIV_NList_Insert_Sorted);
  92.             }
  93.             break;
  94.             }
  95.     }
  96.     return(DoSuperMethodA(cl, obj, msg));
  97. }
  98.  
  99.  
  100. ULONG ChannelListNew(struct IClass *cl, Object *obj, struct opSet *msg)
  101. {
  102.     static const struct Hook ChannelListDispHook = { {NULL, NULL}, &ChannelListDisplay,  NULL, NULL };
  103.     static const struct Hook ChannelListCompHook = { {NULL, NULL}, &ChannelListCompare,  NULL, NULL };
  104.     static const struct Hook ChannelListDestHook = { {NULL, NULL}, &ChannelListDestruct, NULL, NULL };
  105.  
  106.     struct ChannelListData *data;
  107.  
  108.     Object *LV_Channel;
  109.     Object *BT_Update, *BT_Join;
  110.  
  111.     if (obj = (Object *)DoSuperNew(cl, obj,
  112.         MUIA_HelpNode, "channellist",
  113.         MUIA_Window_Title, MSG_CHANNELLIST_TITLE,
  114.         MUIA_Window_ID, MAKE_ID('C','H','L','S'),
  115.         WindowContents, VGroup,
  116.             Child, LV_Channel = NListviewObject,
  117.                 MUIA_NList_Input, TRUE,
  118.                 MUIA_NListview_NList, NListObject,
  119.                     InputListFrame,
  120.                     MUIA_NList_ListBackground, MUII_ListBack,
  121.                     MUIA_NList_Title, TRUE,
  122.                     MUIA_NList_Format, "BAR,BAR,",
  123.                     MUIA_NList_DisplayHook, &ChannelListDispHook,
  124.                     MUIA_NList_CompareHook2, &ChannelListCompHook,
  125.                     MUIA_NList_DestructHook, &ChannelListDestHook,
  126.                 End,
  127.             End,
  128.             Child, HGroup,
  129.                 Child, BT_Join   = SimpleButton(MSG_CHANNELLIST_JOIN_GAD),
  130.                 Child, BT_Update = SimpleButton(MSG_CHANNELLIST_UPDATE_GAD),
  131.             End,
  132.         End,
  133.         TAG_MORE, msg->ops_AttrList))
  134.     {
  135.         data = INST_DATA(cl, obj);
  136.         data->LV_Channel  = LV_Channel;
  137.  
  138.         DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  139.  
  140.         DoMethod(BT_Update, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, CHANLIST_UPDATE);
  141.         DoMethod(BT_Join,   MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, CHANLIST_JOIN);
  142.  
  143.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_TitleClick,  MUIV_EveryTime, LV_Channel, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_Both);
  144.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_TitleClick2, MUIV_EveryTime, LV_Channel, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_2);
  145.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_SortType,    MUIV_EveryTime, LV_Channel, 3, MUIM_Set, MUIA_NList_TitleMark,  MUIV_TriggerValue);
  146.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_SortType2,   MUIV_EveryTime, LV_Channel, 3, MUIM_Set, MUIA_NList_TitleMark2, MUIV_TriggerValue);
  147.  
  148.         return((ULONG)obj);
  149.     }
  150.     return NULL;
  151. }
  152.  
  153.  
  154. MUIF ChannelListDisplay(REG(a2) char **array, REG(a1) struct ChannelEntry *entry)
  155. {
  156.     static char users[5];
  157.  
  158.     if (entry) {
  159.         *array++ = entry->Name;
  160.         sprintf(users, "%d", entry->NumUsers);
  161.         *array++ = users;
  162.         *array = entry->Topic;
  163.     }
  164.     else {
  165.         *array++ = (char *)MSG_CHANNELLIST_NAME;
  166.         *array++ = (char *)MSG_CHANNELLIST_USERS;
  167.         *array   = (char *)MSG_CHANNELLIST_TOPIC;
  168.     }
  169.  
  170.     return NULL;
  171. }
  172.  
  173.  
  174. MUIF ChannelListCompare(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_CompareMessage *ncm)
  175. {
  176.     struct ChannelEntry *entry1 = ncm->entry1;
  177.     struct ChannelEntry *entry2 = ncm->entry2;
  178.     LONG col1 = ncm->sort_type & MUIV_NList_TitleMark_ColMask;
  179.     LONG col2 = ncm->sort_type2 & MUIV_NList_TitleMark2_ColMask;
  180.     ULONG result = 0;
  181.  
  182.     if (ncm->sort_type == MUIV_NList_SortType_None) return (0);
  183.  
  184.     if (col1 == 0) {
  185.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  186.             result = (LONG) stricmp(entry2->Name, entry1->Name);
  187.         else
  188.             result = (LONG) stricmp(entry1->Name, entry2->Name);
  189.     }
  190.     else if (col1 == 1) {
  191.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  192.             result = entry2->NumUsers - entry1->NumUsers;
  193.         else
  194.             result = entry1->NumUsers - entry2->NumUsers;
  195.     }
  196.     else if (col1 == 2) {
  197.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  198.             result = (LONG) stricmp(entry2->Topic, entry1->Topic);
  199.         else
  200.             result = (LONG) stricmp(entry1->Topic, entry2->Topic);
  201.     }
  202.  
  203.     if ((result != 0) || (col1 == col2)) return (result);
  204.  
  205.     if (col2 == 0) {
  206.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  207.             result = (LONG) stricmp(entry2->Name, entry1->Name);
  208.         else
  209.             result = (LONG) stricmp(entry1->Name, entry2->Name);
  210.     }
  211.     else if (col2 == 1) {
  212.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  213.             result = entry2->NumUsers - entry1->NumUsers;
  214.         else
  215.             result = entry1->NumUsers - entry2->NumUsers;
  216.     }
  217.     else if (col2 == 2) {
  218.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  219.             result = (LONG) stricmp(entry2->Topic, entry1->Topic);
  220.         else
  221.             result = (LONG) stricmp(entry1->Topic, entry2->Topic);
  222.     }
  223.  
  224.     return (result);
  225. }
  226.  
  227.  
  228. MUIF ChannelListDestruct(REG(a2) APTR pool, REG(a1) struct ChannelEntry *entry)
  229. {
  230.     free(entry);
  231.     return NULL;
  232. }
  233.